package com.situ.view;

import java.io.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.situ.bean.Book;
import com.situ.bean.Card;
import com.situ.bean.Record;
import com.situ.dao.JDBCUtils;
import com.situ.dao.LibraryDAO;
import com.situ.dao.MyException;

//        1-进书、办理借书卡
//        2-借书和还书功能
//        3-查询某书的借阅记录
//        4-查询谋卡的借阅记录
//        5-按借阅次数，显示图书信息。
public class MainView {
    LibraryDAO lDao = new LibraryDAO();

	public void enterMainMenu() throws SQLException {

		boolean loopFlag = true;
		char menu = 0;

		while (loopFlag) {
			System.out.println("1-按借阅次数显示图书列表        2-进书");
			System.out.println("3-办理借书卡                               4-查询借书卡借阅记录");
			System.out.println("5-借书                                           6-还书");
			System.out.println("7-查询图书借阅记录 ");
			System.out.println("                      0-退出");
			System.out.println("请选择(0-7)：");
			menu = TSUtility.readMenuSelection();

			switch (menu) {
			case '1':
			    getBookList();
				break;
			case '2':
				addBook();
				break;
			case '3':
				addCard();
				break;
			case '4':
			    viewCard();
				break;
			case '5':
				borrowedBook();
				break;
			case '6':
				returnBook();
				break;
			case '7':
				viewBook();
				break;
			case '0':
				System.out.println("确认是否退出Y/N");
				char isExit = TSUtility.readConfirmSelection();
				if (isExit == 'Y') {
					loopFlag = false;
				}
				break;
			default:
			    break;
			}
			System.out.println();
		}
	}

	private void getBookList() throws SQLException {
		System.out.println("---------------------按借阅次数显示图书列表---------------------");
		System.out.println("| 图书ID | 书名 | 作者 | 出版社 | 借阅次数 | 状态");
		List<Book> books = lDao.getAllBook();
		if (books != null) {
		    books.forEach(param -> {
		        System.out.println(param);
		    });            
        }
	}

    private void viewCard() throws SQLException {
        System.out.println("---------------------查询借书卡借阅记录---------------------");
        System.out.print("请输入要查询的借书卡的ID：");
        int cardId = TSUtility.readInt(); // 非空
        ResultSet rs = null;
        try {
            rs = lDao.getCardBords(cardId);
        }catch (MyException e) {
            System.out.println(e.getMessage());
        }
        System.out.println("图书ID\t借书卡ID\t借出时间\t\t\t归还时间");
        SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        
        while (rs.next()) {
            String temp = "";
            if (rs.getDate(4) == null) {
               temp = rs.getInt(1) + "\t" + rs.getString(2) + "\t" + fm.format(rs.getTimestamp(3)) + "\t" + "无";
            }else {
               temp = rs.getInt(1) + "\t" + rs.getString(2) + "\t" + fm.format(rs.getTimestamp(3)) + "\t" + fm.format(rs.getTimestamp(4));                
            }
            System.out.println(temp);
        }
        rs.close();
        JDBCUtils.disconnects();

	}
    
    private void viewBook() throws SQLException {
        System.out.println("---------------------查询图书借阅记录---------------------");
        System.out.print("请输入要查询的图书的ID：");
        int bookId = TSUtility.readInt(); // 非空
        ResultSet rs = null;
        try {
            rs = lDao.getBookBords(bookId);
        }catch (MyException e) {
            System.out.println(e.getMessage());
        }
        System.out.println("借书卡ID\t借书人ID\t借出时间\t\\t\\t归还时间");
        SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        while (rs.next()) {
            String temp = "";
            if (rs.getDate(4) == null) {
               temp = rs.getInt(1) + "\t" + rs.getString(2) + "\t" + fm.format(rs.getTimestamp(3)) + "\t" + "无";
            }else {
               temp = rs.getInt(1) + "\t" + rs.getString(2) + "\t" + fm.format(rs.getTimestamp(3)) + "\t" + fm.format(rs.getTimestamp(4));                
            }
            System.out.println(temp);
        }
        rs.close();
        JDBCUtils.disconnects();

    }



	private void returnBook() throws SQLException {
        System.out.println("---------------------借书---------------------");
        System.out.print("请输入要还的图书的ID：");
        int bookId = TSUtility.readInt(); // 非空
        try {
            if (lDao.returnBook(bookId) > 0) {
                System.out.println("还书成功");
            }
        } catch (MyException e) {
            System.out.println(e.getMessage());
        }
        // 回车继续
        TSUtility.readReturn();

	}

	private void borrowedBook() throws SQLException {
		System.out.println("---------------------借书---------------------");
		System.out.print("请输入要借的图书的ID：");
		int bookId = TSUtility.readInt(); // 非空
		System.out.print("请输入您的借书卡ID：");
        int cardId = TSUtility.readInt(); // 非空
        try {
            if (lDao.insertRecord(bookId, cardId) > 0) {
                System.out.println("借书成功");
            }else {
                System.out.println("借书失败");
            }
        } catch (MyException e) {
            System.out.println(e.getMessage());
        }
        // 回车继续
        TSUtility.readReturn();
    }

//
//	// 遍历图书
//	private void getBooks() {
//		Book[] books = bookSvc.getBooks();
//		System.out.println("ID\t书名\t作者\t出版社\t状态\t借阅次数");
//		for (int i = 0; i < books.length; i++) {
//			System.out.println(books[i]);
//		}
//	}
//
//	// 遍历借书卡
//	private void getPersons() {
//		Person[] persons = personSvc.getPersons();
//		System.out.println("ID\t姓名");
//		for (int i = 0; i < persons.length; i++) {
//			System.out.println(persons[i]);
//		}
//	}
//
	// 进书
	private void addBook() throws SQLException {
		System.out.println("---------------------进书---------------------");
		System.out.print("请输入要添加的图书ID：");
		int bookId = TSUtility.readInt(); // 非空
		System.out.print("请输入要添加的图书名：");
		String bokkName = TSUtility.readKeyBoard(34, false); // 非空
		System.out.print("请输入要添加的图书作者(选填)：");
		String author = TSUtility.readKeyBoard(34, true); // 可空
		System.out.print("请输入要添加的图书出版社(选填)：");
        String press = TSUtility.readKeyBoard(34, true); // 可空

        Book b = new Book(bookId, bokkName, author, press);
        try {
            if (lDao.insertBook(b) > 0) {
                System.out.println("添加成功");
            } else {
                System.out.println("添加失败");
            }
        } catch (MyException e) {
            System.out.println(e.getMessage());
        }
        // 回车继续
        TSUtility.readReturn();
    }

	// 办理借书卡
	private void addCard() throws SQLException {
		System.out.println("---------------------办理借书卡---------------------");
		System.out.print("请输入要办理的借书卡ID：");
		int id = TSUtility.readInt(); // 非空
		System.out.print("请输入要办理借书卡的用户名：");
        String name = TSUtility.readKeyBoard(34, false); // 非空

        Card card = new Card(id, name);
        try {
            if (lDao.insertCard(card) > 0) {
                System.out.println("添加成功");            
            }else {
                System.out.println("添加失败");            
            }
        } catch (MyException e) {
            System.out.println(e.getMessage());
        }
        // 回车继续
        TSUtility.readReturn();
    }

	public static void main(String[] args) throws FileNotFoundException, IOException, SQLException {
		MainView view = new MainView();
		
		view.enterMainMenu();

     
	}

}
